package com.example.holes;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class LevelSelectActivity extends Activity{
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levelselect);
    }
	
	private void launchGame(){
		Intent intent = new Intent(this, GameActivity.class);
        startActivityForResult(intent, 1); // get score
	}
    
    public void launch_01(View view) {
    	GameState.level = 1;
    	launchGame();
    }
    
    public void launch_02(View view) {
    	GameState.level = 2;
    	launchGame();
    }
    
    public void launch_03(View view) {
    	GameState.level = 3;
    	launchGame();
    }
    
    public void launch_04(View view) {
    	GameState.level = 4;
    	launchGame();
    }
    
    public void launch_05(View view) {
    	GameState.level = 5;
    	launchGame();
    }
    
    public void launch_06(View view) {
    	GameState.level = 6;
    	launchGame();
    }
    
    public void launch_07(View view) {
    	GameState.level = 7;
    	launchGame();
    }
    
    public void launch_08(View view) {
    	GameState.level = 8;
    	launchGame();
    }
    
    public void launch_09(View view) {
    	GameState.level = 9;
    	launchGame();
    }
    
    public void launch_10(View view) {
    	GameState.level = 10;
    	launchGame();
    }
    
    // getting final score from GameActivity
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	System.gc(); // clear the game engine from heap
    	if (requestCode == 1) {
    		if(resultCode == RESULT_OK){   
    			int result = data.getIntExtra("score", 0);
    			Toast.makeText(this, "Final score:" + result, Toast.LENGTH_SHORT).show();
    			if (data.getBooleanExtra("completed", false)){
    				levelCompleted();
    			}
    		}
    	    if (resultCode == RESULT_CANCELED) {    
    	         //Write your code if there's no result
    	    }
    	}
    }
    
    // replace the button's image with a checkmark
    private void levelCompleted(){
    	ImageButton button = null;
		switch(GameState.level){
        	case 1: button = (ImageButton) findViewById(R.id.imageButton1);
        			break;
        	case 2: button = (ImageButton) findViewById(R.id.imageButton2);
    				break;
        	case 3: button = (ImageButton) findViewById(R.id.imageButton3);
    				break;
        	case 4:	button = (ImageButton) findViewById(R.id.imageButton4);
    				break;
        	case 5:	button = (ImageButton) findViewById(R.id.imageButton5);
    				break;
        	case 6:	button = (ImageButton) findViewById(R.id.imageButton6);
    				break;
        	case 7:	button = (ImageButton) findViewById(R.id.imageButton7);
    				break;
        	case 8:	button = (ImageButton) findViewById(R.id.imageButton8);
    				break;
        	case 9:	button = (ImageButton) findViewById(R.id.imageButton9);
    				break;
        	case 10:button = (ImageButton) findViewById(R.id.imageButton10);
    				break;
    	}
		
		button.setImageResource(R.drawable.level_check);
	}
}
